package cn.echoedu.specifics;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.ClassNotify;
import cn.echoedu.entity.ClassNotifyImage;
import cn.echoedu.entity.Image;
import cn.echoedu.entity.Student;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class SpecClassNotifyImage extends BaseSpec{
	private final String T = "SpecClassNotifyImage----->";
	@Override
	public <T> PreparedStatement getPreparedInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		ClassNotifyImage classnotifyimage = (ClassNotifyImage)currentPojo;
		final String M = "getPreparedInsertRunning**********>>";
		L.p(T+M);
		String sql  = "insert into classnotifyimage(classnotifyid," +
				"imageid) values"+StringUtil.getBeforeTheBsrackets()
				+"?,?"+StringUtil.getAfterTheBrackets();
		PreparedStatement preparedstatement = con.prepareStatement(sql);
		preparedstatement.setInt(1,classnotifyimage.getClassnotify().getClassnotify());
		preparedstatement.setInt(2,classnotifyimage.getImageid().getImageid());
		return preparedstatement;
	}

	@Override
	public Object getPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		final String M = "getPojo********";
		L.p(T+M);
		ClassNotifyImage classnotifyimage = new ClassNotifyImage(); 
		classnotifyimage.setClassnotifyimageid(rs.getInt(1));
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(rs.getInt(2));
		classnotifyimage.setClassnotify(classnotify);
		Image image = new Image();
		image.setImageid(rs.getInt(3));
		L.p(T+M+image.getImageid());
		classnotifyimage.setImageid(image);
		return classnotifyimage;
	}

	@Override
	public <T> PreparedStatement getPreparedUpdate(Connection con,
			TableName tableName, T oldPojo, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> String getPreparedInsertGetPrimaryKeySql(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getLeftJoinPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return pojoRs(rs);
	}

	@Override
	public Object getPojoBySql(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		final String M  = "getPojoBySql**********>>";
		L.p(T+M);

		return pojoRs(rs);
	}

	@Override
	public <T> PreparedStatement getPreparedListInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		List<ClassNotifyImage> classnotifyimagelist =
				(List<ClassNotifyImage>)currentPojo;
		String sql = "insert into classnotifyimage(classnotifyid,imageid) values" +
				"(?,?)";
		PreparedStatement preparedstatement = con.prepareStatement(sql
				,PreparedStatement.RETURN_GENERATED_KEYS);
		for(ClassNotifyImage classnotifyimage:classnotifyimagelist){
			if(classnotifyimage!=null&&classnotifyimage.getClassnotify()!=null
					&&classnotifyimage.getClassnotify().getClassnotify()!=0){
				preparedstatement.setInt(1,classnotifyimage.getClassnotify().getClassnotify());
			}else{
				preparedstatement.setNull(2,Types.INTEGER);
			}
			if(classnotifyimage!=null&&classnotifyimage.getImageid()!=null
					&&classnotifyimage.getImageid().getImageid()!=0){
				preparedstatement.setInt(2,classnotifyimage.getImageid().getImageid());
			}else{
				preparedstatement.setNull(2,Types.INTEGER);
			}
			preparedstatement.addBatch();
		}

		return preparedstatement;
	}
	private ClassNotifyImage pojoRs(ResultSet rs) throws SQLException{
		ClassNotifyImage classnotifyimage = new ClassNotifyImage();
		classnotifyimage.setClassnotifyimageid(
				rs.getInt("classnotifyimage.classnotifyimageid"));
		ClassNotify classnotify = new ClassNotify();
		try{
			classnotify.setClassnotify(rs.getInt("classnotify.classnotifyid"));
			classnotify.setTitle(rs.getString("classnotify.title"));
			classnotify.setPlace(rs.getString("classnotify.place"));
			classnotify.setContent(rs.getString("classnotify.content"));
			classnotify.setFilepath(rs.getString("classnotify.filepath"));
			classnotify.setStarttime(rs.getTimestamp("classnotify.starttime"));
			classnotify.setEndtime(rs.getTimestamp("classnotify.endtime"));
			classnotify.setRemindertime(rs.getTimestamp("classnotify.remindertime"));
			classnotify.setShowinhomepage(rs.getInt("classnotify.showinhomepage"));
			classnotify.setHitcount(rs.getInt("classnotify.hitcount"));
			classnotify.setPublishtime(rs.getTimestamp("classnotify.publishtime"));
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			Student student = new Student();
			student.setStudentnumber(rs.getString("classnotify.studentnumber"));
			classnotify.setStudent(student);
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			AClass aclass = new AClass();
			aclass.setClasscode(rs.getInt("classnotify.classcode"));
			classnotify.setAclass(aclass);
			classnotify.setLikenum(rs.getInt("classnotify.likenum"));
			classnotify.setDislikenum(rs.getInt("classnotify.dislikenum"));
			classnotifyimage.setClassnotify(classnotify);
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			Image image = new Image();
			image.setImageid(rs.getInt("image.imageid"));
			image.setImagename(rs.getString("image.imagename"));
			image.setImagesrc(rs.getString("image.imagesrc"));
			classnotifyimage.setImageid(image);
		}catch(Exception e){
			e.printStackTrace();
		}
		return classnotifyimage;
	}
}
